package com.ruoyi.wisdom.event;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.tool.TimerUtils;
import com.ruoyi.wisdom.constants.IotConstants;
import com.ruoyi.wisdom.domain.IotConfig;
import com.ruoyi.wisdom.domain.IotPickRecord;
import com.ruoyi.wisdom.domain.IotPickScene;
import com.ruoyi.wisdom.domain.method.PushTemplateMessagData;
import com.ruoyi.wisdom.domain.method.PushTemplateMessage;
import com.ruoyi.wisdom.domain.other.OtherOptions;
import com.ruoyi.wisdom.domain.swap.SwapPickFace;
import com.ruoyi.wisdom.domain.swap.SwapPickRecord;
import com.ruoyi.wisdom.domain.swap.SwapStudentInfo;
import com.ruoyi.wisdom.service.IIotPickRecordService;
import com.ruoyi.wisdom.service.IIotPickSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PickRecordEvent extends IotEvent {

    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;

    @Autowired
    @Qualifier("redisSecondTemplate")
    public RedisTemplate redisSecondTemplate;

    @Autowired
    private IIotPickRecordService iotPickRecordService;

    @Autowired
    private MessagePushEvent messagePushEvent;

    @Autowired
    private IIotPickSceneService iotPickSceneService;

    /**
     * 推送数据
     */
    public void push() {
        IotPickRecord params = new IotPickRecord();
        params.setSyn(IotConstants.RECORD_SYNC_NO);
        List<IotPickRecord> rows = iotPickRecordService.selectIotPickRecordList(params);
        if (rows.size() > 0) {
            for (IotPickRecord row : rows) {
                // 实体转实体
                SwapPickRecord swapPickRecord = getModelMapper().map(row, SwapPickRecord.class);
                Long result = redisSecondTemplate.opsForList().rightPush(iotConstants.PUSH_RECORD, JSONObject.toJSONString(swapPickRecord));
                if (result > 0) {
                    row.setSyn(IotConstants.RECORD_SYNC_YES);
                    iotPickRecordService.updateIotPickRecord(row);
                }
            }
        }
    }

    /**
     * 删除数据
     * [删除三个月前数据]
     */
    public void delete() {
        String endTime = TimerUtils.getBeforeTime(3, "MONTH", "yyyy-MM-dd HH:mm:ss");
        IotPickRecord params = new IotPickRecord();
        params.setEndTime(endTime);
        iotPickRecordService.deleteIotPickRecordByParams(params);
    }

    /**
     * 出入消息推送
     */
    public void pushMessage() {
        Object iotConfigObj = redisTemplate.opsForValue().get(IotConstants.SYSTEM_CONFIG);
        IotConfig iotConfig = JSON.parseObject(iotConfigObj.toString(), IotConfig.class);
        if (iotConfig != null && iotConfig.getIsPushMessage() > 0) {
            String endTime = TimerUtils.getBeforeTime(20, "MINUTE", "yyyy-MM-dd HH:mm:ss");
            IotPickRecord params = new IotPickRecord();
            params.setEndTime(endTime);
            params.setIsPush(IotConstants.RECORD_PUSH_NO);
            List<IotPickRecord> rows = iotPickRecordService.selectIotPickRecordList(params);
            if (rows.size() > 0) {
                // 推送微信推送消息
                for (IotPickRecord row : rows) {
                    messagePushEvent.pushPickRecordMessage(row);
                }
            }
        }
    }

    /**
     * 测试消息推送
     */
    public void test() {
        long id = 1;
        IotPickRecord row = iotPickRecordService.selectIotPickRecordById(id);
        messagePushEvent.pushPickRecordMessage(row);
    }
}
